<?php
class Mail_TemplateController extends Promotor_Controller_Action_Scaffold {
	public $skin = array(
		// forsuje domyslny template i style na admin
		'template' => 'admin',
		'style' => 'default',
		// layout
		'layout' => 'admin_mail',
		// rozszerzam konfiguracje
		'config' => array(
			'filename' => 'backend_config.ini'
		)
	);
	
	public $acl = array(
		'list' => 'list',
		'add' => 'create',
		'edit' => 'update',
		'delete' => 'delete'
	);
	
	public $ajaxable = array(
		'list' => array('json')
	);

	/**
	 * @see Promotor_Controller_Action_Scaffold specialization
	 */
	protected $_modelClass = 'Mail_Model_Template';
	protected $_formAddClass = 'Mail_Form_TemplateAdd';
	protected $_formEditClass = 'Mail_Form_TemplateEdit';
	protected $_formRemoveClass = 'Mail_Form_TemplateRemove';
	
	/**
	 * Inicjuje kontroler.. 
	 * @return void
	 */
	public function init() {
		/* @var $ajaxContext Zend_Controller_Action_Helper_AjaxContext */
		$ajaxContext = $this->_helper->getHelper('AjaxContext');
		$ajaxContext->setAutoJsonSerialization(false);
		$ajaxContext->initContext();
	}
	
	/**
	 * Wypisz szablony
	 * @return void
	 */
	public function listAction() {
		/* @var $rq Zend_Controller_Request_Http */
		$rq = $this->getRequest();

		$config = $this->_helper->config('template.xml');

		$model = new Mail_Model_Template();

		// są edytowanle opcje
		if ($rq->isPost()) {
			$action = true;

			switch ($rq->getPost('action_type')) {
				case 'update':
					if (null !== $rq->getPost('editable')) {
						if ($this->_helper->acl->isAllowed('update')) {
							$data = $rq->getPost('editable');
							$model->editableUpdate($data);
						}
					}
					break;
				case 'delete':
					if (null !== $rq->getPost('action_checked')) {
						if ($this->_helper->acl->isAllowed('delete')) {
							$data = $rq->getPost('action_checked');
							$model->editableDelete($data);
						}
					}
					break;
				default:
					$action = false;
			}
			
			// wyślij wiadomości i przekieruj
			if ($action) {
				/* @var $flashMessenger Zend_Controller_Action_Helper_FlashMessenger */
				$flashMessenger = $this->_helper->getHelper('FlashMessenger');
				$flashMessenger->addMessage($model->getStatus());
				array_map(array($flashMessenger,'addMessage'), $model->getMessages(true));
			}
		}

		// setup data grid
		$grid = KontorX_DataGrid::factory($model->selectList(), $config->grid);
		$grid->setValues($rq->getQuery());

		$this->_setupDataGridPaginator($grid);

		$this->view->grid = $grid;
	}
}